package com.maridress.maridress.application;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.maridress.maridress.constant.Constant;
import com.maridress.maridress.dependencies.ApiComponent;
import com.maridress.maridress.dependencies.DaggerApiComponent;
import com.maridress.maridress.dependencies.network.DaggerNetworkComponent;
import com.maridress.maridress.dependencies.network.NetworkComponent;
import com.maridress.maridress.dependencies.network.NetworkModule;

import my.mumayizblive.yzblib.ZBGetAppKeyListener;
import my.mumayizblive.yzblib.ZBLoadResultCallback;
import my.mumayizblive.yzblib.ZBUtils;

/**
 * Created by tenmei on 16/9/6.
 */
public class MariDressApplication extends Application {
    private static Context mContext;
    private ApiComponent mApiComponent;

    @Override
    public void onCreate() {
        resolveDependency();
        super.onCreate();

        ZBUtils.setDebug(true);
        mContext = getApplicationContext();

        ZBUtils.init(this, new ZBLoadResultCallback() {
            @Override
            public void success() {
//                System.out.println(" 初始化成功");

                ZBUtils.getAppKey(MariDressApplication.this, new ZBGetAppKeyListener() {
                    @Override
                    public void getAppKey(String appKey) {
//                        System.out.println(" 加载成功 获取 appKey:" + appKey);
                    }
                });
//                        SharePrefUtil.saveString(MyApplication.this, "zb_mumayi_appkey", "45678979845466611");
                ZBUtils.setAppKey(MariDressApplication.this, "94d46a1c025ce95177Jg9WTqJCshWlBQ4EuKWJMNGQOGPOyDLAy4oqY1Q0fXiOAvUw");
//                System.out.println("  加载成功 设置appKey");
                ZBUtils.getAppKey(MariDressApplication.this, new ZBGetAppKeyListener() {
                    @Override
                    public void getAppKey(String appKey) {
//                        System.out.println(" 加载成功 获取 appKey2:" + appKey);
                    }
                });
            }

            @Override
            public void failed(String s) {
            }
        });


//        LeakCanary.install(this);
    }

    private void resolveDependency() {
        mApiComponent = DaggerApiComponent.builder()
                .networkComponent(getNetworkComponent())
                .build();
    }

    public NetworkComponent getNetworkComponent() {
        return DaggerNetworkComponent.builder()
                .networkModule(new NetworkModule(Constant.BASE_URL))
                .build();
    }

    public ApiComponent getApiComponent() {
        return mApiComponent;
    }

    public static Context getContext() {
        return mContext;
    }


    class ShareBraodcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("  收到分享信息了");
//            int isfocus = 0;
//            try {
//                isfocus = intent.getIntExtra("isfocus", 0);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            String playurl = intent.getStringExtra("playurl");
//            String playurl = "0.0-" + isfocus;
            System.out.println("  收到分享信息了：" + playurl);
            Toast.makeText(context, "收到分享了" + playurl, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        ZBUtils.initAttachBaseContext(base);
        super.attachBaseContext(base);
    }
}
